package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.detail;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.entity.mg.res.AllTemplate;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;

/**
 * 作业详情-作答详情(填空题, 简单题, 通知类, 阅读类-弹框)
 */
@CrossOrigin
@RestController
public class HomeworkDetailQues2345StuFindController {

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    ElasticQuesService elasticQuesService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/detail/ques2345/stu/people/find", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            @RequestParam String quesId, //
            RespModel respModel) throws ParseException {

        // 获得原题
        int optionlength = 0;
        try {
            ElasticYxyYxyQuesResource es = elasticQuesService.findByEsQuesId(quesId);
            optionlength = ((AllTemplate) es.getQuesBody()).sub.branch.size();
        } catch (Exception e) {
        }

        // 获取班级这道题的作答
        List<HomeworkTaskReportQues> homeworkTaskReportQuesLst = homeworkTaskReportQuesRepository
                .findByTaskIdAndClassIdAndQuesId(taskId, classId, quesId);

        // return
        // ##
        List<SelectPeople> selectPeoples = new ArrayList<SelectPeople>();
        for (int i = 0; i <= 5; i++) {
            // ##
            int total = 0;
            List<String> studentNames = new ArrayList<String>();

            for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQuesLst) {
                // ##
                if (homeworkTaskReportQues.getMark() != null && i == homeworkTaskReportQues.getMark()) {
                    total++;
                    studentNames.add(homeworkTaskReportQues.getStudentName());
                }
            }
            // return
            SelectPeople selectPeople = new SelectPeople();
            selectPeople.score = i + "";
            selectPeople.total = total;
            selectPeople.studentNames = studentNames;
            selectPeoples.add(selectPeople);
        }
        respModel.selectPeoples = selectPeoples;

        // ##
        Integer total = 0;
        Integer totalExactnes = 0;
        for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQuesLst) {
            if (homeworkTaskReportQues.getMark() != null) {
                total = total + 5;
                totalExactnes = totalExactnes + homeworkTaskReportQues.getMark();
            }
        }
        if (total > 0 && totalExactnes > 0) {
            double accuracy = (totalExactnes.doubleValue() / total.doubleValue()) * 100;
            respModel.scoreAverage = rightRate(accuracy);
        }
        respModel.score = total;
        respModel.duoScore = totalExactnes;
        return respModel;
    }

    /** 响应 **/
    protected static class RespModel extends YxyApiRespModel {
        // 得分率
        public String scoreAverage = "0%";
        // 选择人数（人）
        public List<SelectPeople> selectPeoples;
        // 分数
        public int score = 0;
        // 应得分数
        public int duoScore = 0;
    }

    public static class SelectPeople {
        // 分数
        public String score;
        // 人数
        public int total;
        // 学生名单
        public List<String> studentNames;
    }

    private Double getDouble(Double x1) {
        if (x1 == null) { return 0d; }
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.#");
        double parse = Double.parseDouble(df.format(x1));
        return parse;
    }

    private String rightRate(Double x1) {
        if (x1 == null) { return "0%"; }
        String rightRate = this.getDouble(x1).toString().concat("%");
        return rightRate.replace(".0%", "%");
    }
}